package com.github.prontera.controller;

import java.time.OffsetDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.RequestLogging;
import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.ClientRent;
import com.github.prontera.domain.TalentOrder;
import com.github.prontera.domain.enums.OrderAcceptStatusEnum;
import com.github.prontera.domain.enums.OrderStatusEnum;
import com.github.prontera.domain.enums.PayStatusEnum;
import com.github.prontera.model.request.TalentOrderRequest;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.service.ClientRentService;
import com.github.prontera.service.ClientService;
import com.github.prontera.service.OrderRefundService;
import com.github.prontera.service.TalentOrderService;
import com.github.prontera.util.OrderNoGenerator;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/talentOrder")
@Api(value = "达人订单")
public class TalentOrderController {

	@Autowired
	private TalentOrderService talentOrderService;
	@Autowired
	private ClientRentService clientRentService;
	@Autowired
	private ClientService clientService;
	@Autowired
	private OrderRefundService orderRefundService;
	
	 
	/**
	 * 添加达人订单
	 */
	
	@ApiOperation(value = "添加达人订单",notes = "添加达人订单")
	@ResponseBody
	@RequestLogging
	@RequestMapping(value = "insert", method = RequestMethod.POST)
	public ObjectDataResponse<TalentOrder> insert(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrder order) {
		if(order.getAcceptClientId()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "acceptClientId必传!");
		}
		if(order.getClientRentId()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "clientRentId必传!");
		}
		if(order.getAmount()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "amount必传!");
		}
		ClientRent clientRent=clientRentService.get(order.getClientRentId());
		if(clientRent==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "clientRentId必传!");
		}
		if(order.getTimeDuration()==null||order.getTimeDuration()<1 || order.getTimeDuration()>48) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "timeDuration错误!");
		}
		if(order.getAmount().doubleValue()!=(clientRent.getAmount().doubleValue()*order.getTimeDuration())) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "请输入正确得amount值!");
		}
		order.setSkills(clientRent.getSkill());
		order.setTradeNo(new OrderNoGenerator().getString());
		order.setClientId(clientId);
		order.setCompletionTime(order.getStartTime().minusDays(order.getTimeDuration()));
		order.setAcceptLastUpdateTime(null);
		order.setAcceptStatus(OrderAcceptStatusEnum.WAIT_ACCEPT);
		order.setStatus(OrderStatusEnum.CONFIRMED);
		order.setComment(null);
		order.setUpdateTime(null);
		order.setPayStatus(PayStatusEnum.WAIT_PAYMENT);
		if(talentOrderService.insertOrder(order)){
			return new ObjectDataResponse<TalentOrder>(order);
		}
		return new ObjectDataResponse<TalentOrder>(new TalentOrder());
	}
	
	
	/**
	 * 获取查找达人订单
	 */
	
	@ApiOperation(value = "获取查找达人订单",notes = "获取查找达人订单")
	@ResponseBody
	@RequestLogging
	@RequestMapping(value = "get", method = RequestMethod.POST)
	public ObjectDataResponse<TalentOrder> get(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrderRequest talentOrderRequest) {
		TalentOrder findOrder=new TalentOrder();
		BeanUtils.copyProperties(talentOrderRequest, findOrder);
		List<TalentOrder> talentOrders=null;
		if(talentOrderRequest.isHasConsume()){//是消费者
			findOrder.setAcceptClientId(talentOrderRequest.getClientId());
			findOrder.setClientId(null);
			talentOrders=talentOrderService.findAcceptClientOrder(findOrder);
		}else{
			findOrder.setClientId(talentOrderRequest.getClientId());
			findOrder.setAcceptClientId(null);
			talentOrders=talentOrderService.findClientOrder(findOrder);
		}
		if(talentOrders==null || talentOrders.size()==0){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误");
		}
		return new ObjectDataResponse<TalentOrder>(talentOrders.get(0));
	}
	
	
	/**
	 * 查找达人订单
	 */
	
	@ApiOperation(value = "查找达人订单",notes = "查找达人订单")
	@ResponseBody
	@RequestMapping(value = "find", method = RequestMethod.POST)
	public ObjectDataResponse<List<TalentOrder>> find(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrderRequest talentOrderRequest) {
		TalentOrder findOrder=new TalentOrder();
		BeanUtils.copyProperties(talentOrderRequest, findOrder);
//		if(talentOrderRequest.getStatus()==OrderStatusEnum.CARRY){
//			findOrder.setStatus(null);
//			List<OrderStatusEnum> statusEnums=new ArrayList<OrderStatusEnum>();
//			statusEnums.add(OrderStatusEnum.FINISHED);
//			statusEnums.add(OrderStatusEnum.REFUNDS);
//			findOrder.setNotInStatus(statusEnums);
//		}
		findOrder.setHasComment(talentOrderRequest.getHasComment());
		findOrder.setOrderBy("O_TALENT_ORDER.create_time desc");
		if(talentOrderRequest.isHasConsume()){
			findOrder.setClientId(clientId);
			return new ObjectDataResponse<List<TalentOrder>>(talentOrderService.findAcceptClientOrder(findOrder));
		}else{
			findOrder.setAcceptClientId(clientId);
			return new ObjectDataResponse<List<TalentOrder>>(talentOrderService.findClientOrder(findOrder));
		}
	}
	
	/**
	 * 更新超时未接受订单
	 */
	
	@ApiOperation(value = "更新超时未接受订单",notes = "更新超时未接受订单")
	@ResponseBody
	@RequestMapping(value = "updateTimeoutOrder", method = RequestMethod.POST)
	public ObjectDataResponse<Boolean> updateTimeoutOrder(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrder talentOrder) {
		if(talentOrder.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误!");
		}
		TalentOrder findOrder=talentOrderService.get(talentOrder.getId());
		if(talentOrder.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "订单不存在!");
		}
		findOrder.setReason("未接受超时订单");
		findOrder.setAcceptStatus(OrderAcceptStatusEnum.UNACCEPTED);
		findOrder.setStatus(OrderStatusEnum.FINISHED);
		talentOrderService.updateTimeoutOrder(findOrder);
		return new ObjectDataResponse<Boolean>(true);
	}
	
	/**
	 * 更新超时未支付订单
	 */
	
	@ApiOperation(value = "更新超时未支付订单",notes = "更新超时未支付订单")
	@ResponseBody
	@RequestMapping(value = "updateNotStartTimeoutOrder", method = RequestMethod.POST)
	public ObjectDataResponse<Boolean> updateNotStartTimeoutOrder(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrder talentOrder) {
		if(talentOrder.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误!");
		}
		TalentOrder findOrder=talentOrderService.get(talentOrder.getId());
		if(talentOrder.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "订单不存在!");
		}
		findOrder.setReason("未付款超时订单");
		findOrder.setAcceptStatus(OrderAcceptStatusEnum.UNACCEPTED);
		findOrder.setStatus(OrderStatusEnum.FINISHED);
		talentOrderService.update(findOrder);
		return new ObjectDataResponse<Boolean>(true);
	}
	
	/**
	 * 更新超时未支付订单
	 */
	
	@ApiOperation(value = "更新超时未支付订单",notes = "更新超时未支付订单")
	@ResponseBody
	@RequestMapping(value = "updateNotEndTimeoutOrder", method = RequestMethod.POST)
	public ObjectDataResponse<Boolean> updateNotEndTimeoutOrder(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrder talentOrder) {
		if(talentOrder.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误!");
		}
		TalentOrder findOrder=talentOrderService.get(talentOrder.getId());
		if(talentOrder.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "订单不存在!");
		}
		findOrder.setReason("未确认超时订单");
		findOrder.setAcceptStatus(OrderAcceptStatusEnum.FINISHED);
		findOrder.setStatus(OrderStatusEnum.FINISHED);
		talentOrderService.update(findOrder);
		return new ObjectDataResponse<Boolean>(true);
	}
	
	
	/**
	 * 更新达人订单
	 */
	@ApiOperation(value = "更新达人订单",notes = "更新达人订单")
	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public ObjectDataResponse<Boolean> update(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrder talentOrder) {
		if(talentOrder.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误!");
		}
		TalentOrder findOrder=talentOrderService.get(talentOrder.getId());
		if(talentOrder.getPayStatus()!=null&&talentOrder.getPayStatus().getCode()<findOrder.getPayStatus().getCode()){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"PayStatus错误");
		}
		if(talentOrder.getAcceptStatus()!=null&&talentOrder.getAcceptStatus().getCode()<findOrder.getAcceptStatus().getCode()){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"AcceptStatus错误");
		}
		if(talentOrder.getStatus()!=null&&talentOrder.getStatus().getCode()<findOrder.getStatus().getCode()){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"Status错误");
		}
		if (talentOrder.getStatus()==OrderStatusEnum.FINISHED&&findOrder.getStatus()!=talentOrder.getStatus()) {//消费者已经结束
			clientService.confirmTalent(findOrder);
			return new ObjectDataResponse<Boolean>(true);
		}
		TalentOrder updateOrder=new TalentOrder();
		updateOrder.setId(findOrder.getId());
		updateOrder.setScore(talentOrder.getScore());
		if (talentOrder.getAcceptStatus()==OrderAcceptStatusEnum.REFUNDS&&findOrder.getAcceptStatus()!=talentOrder.getAcceptStatus()) {//退款中
			updateOrder.setRefundPaymentApplyTime(OffsetDateTime.now());
			updateOrder.setAcceptStatus(OrderAcceptStatusEnum.REFUNDS);
			updateOrder.setStatus(talentOrder.getStatus());
			orderRefundService.refundTalendOrder(updateOrder);
			return new ObjectDataResponse<Boolean>(true);
		}
		//更新订单状态
		if(talentOrder.getStatus()==OrderStatusEnum.CARRY&&findOrder.getStatus()!=talentOrder.getStatus()){//进行中
			updateOrder.setStatus(OrderStatusEnum.CARRY);
			updateOrder.setAcceptStatus(OrderAcceptStatusEnum.CARRY);
		}else if (talentOrder.getStatus()==OrderStatusEnum.REFUNDS&&findOrder.getStatus()!=talentOrder.getStatus()) {//退款
			updateOrder.setCompletionTime(OffsetDateTime.now());
			updateOrder.setStatus(OrderStatusEnum.REFUNDS);
			updateOrder.setAcceptStatus(OrderAcceptStatusEnum.REFUNDS);
		}else if (talentOrder.getAcceptStatus()==OrderAcceptStatusEnum.UNACCEPTED&&findOrder.getAcceptStatus()!=talentOrder.getAcceptStatus()) {//未接受
			updateOrder.setStatus(OrderStatusEnum.FINISHED);
			updateOrder.setAcceptStatus(OrderAcceptStatusEnum.UNACCEPTED);
		}
		if(talentOrderService.update(updateOrder)<=0){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "更新订单状态错误");
		}
		talentOrderService.updateMemoryOrder(updateOrder);
		return new ObjectDataResponse<Boolean>(true);
	}

}
